<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>服务费用</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <link rel="stylesheet" href="../../../../css/mui.min.css">
    <link rel="stylesheet" href="../../../../css/global.css">
    <link rel="stylesheet" href="../../../../css/style.css">
    <style>
        input[readonly] {
            color: #ccc;
        }
    </style>
</head>

<body>
<header class="mui-bar mui-bar-nav">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 id="title" class="mui-title">服务费用</h1>
</header>
<div class="mui-content" id="mui-content" style="display: none;">
    <div class="details_con">
        <div hidden>
            <input type="hidden" id="id"/>
            <input type="hidden" id="enginnerId"/>
            <input type="hidden" id="helpId"/>
            <input type="hidden" id="bu"/>
            <input type="hidden" id="city"/>
            <input type="hidden" id="prov"/>
            <input type="hidden" id="chargeHours"/>
            <input type="hidden" id="noticeId"/>
        </div>
        <div class="clum">
            <span class="name">服务编号</span>
            <span class="txt" id="serviceNo"></span>
        </div>
        <div class="clum">
            <span class="name">数量/天/小时<em class="t_red"></em></span>
            <input type="number" id="manHours" min="0" placeholder="请输入数量/天/小时" class="d_ipt"
                   onkeyup="calculateCost()"/>
        </div>
        <div class="clum">
            <span class="name">单价<em class="t_red"></em></span>
            <input type="number" id="manRate" class="d_ipt" placeholder="请输入单价" onkeyup="calculateCost()"/>
        </div>
        <div class="clum">
            <span class="name">总计<em class="t_red">*</em></span>
            <input type="number" id="totalPrice" class="d_ipt" readonly="readonly"/>
        </div>

        <div class="clum" style="display: none;" id="divTravelStayPrice">
            <span class="name">旅途和住宿费<em class="t_red"></em></span>
            <input type="number" id="travelStayPrice" class="d_ipt" placeholder="请输入旅途和住宿费" onkeyup="calculateCost()"/>
        </div>

        <div class="clum" style="display: none;" id="divBasicFee">
            <span class="name">基本费<em class="t_red">*</em></span>
            <input type="number" id="basicFee" class="d_ipt" placeholder="请输入基本费" onkeyup="calculateCost()"/>
        </div>
        <div class="clum" id="divManTotal">
            <span class="name">旅途和人工费<em class="t_red">*</em></span>
            <input type="number" id="manTotal" class="d_ipt" readonly="readonly"/>
        </div>

        <div class="clum" style="display: none;" id="divEspeciallyFee">
            <span class="name">特殊费用<em class="t_red">*</em></span>
            <input type="number" id="especiallyFee" class="d_ipt" onkeyup="calculateCost()"/>
        </div>

        <div class="clum">
            <span class="name">备件总费用<em class="t_red">*</em></span>
            <input type="number" id="partsTotallFee" class="d_ipt" readonly="readonly"/>
        </div>
        <div class="clum">
            <span class="name">总费用<em class="t_red">*</em></span>
            <!--            <span class="name">总费用（含13%税）<em class="t_red">*</em></span>-->
            <input type="number" id="totalBilled" class="d_ipt" readonly="readonly"/>
        </div>

        <div class="line"></div>
        <div id="btn_box" class="btn_box">
            <span id="cancale" class="fl">取消</span>
            <span id="saveBtn" class="fr red">保存</span>
        </div>
        <div style="height: 50px;"></div>
    </div>
</div>

<!-- js代码 start -->

<!-- 基础js start -->
<script src="../../../../js/mui.min.js"></script>
<script src="../../../../js/utils/h.js"></script>
<script src="../../../../js/constant.js"></script>
<script src="../../../../js/common.js"></script>
<script src="../../../../js/utils/base64.js"></script>
<script src="../../../../js/utils/md5.js"></script>
<script src="../../../../js/utils/sortMap.js"></script>
<script src="../../../../js/utils/http.js"></script>
<script src="../../../../js/app.js"></script>
<!-- 基础js end -->

<script type="text/javascript" charset="utf-8">
    var chargeHours = 0;
    var serviceType
    var subServiceType
    var isWarranty
    var zhuangtai = false;
    var shifoujisuan = true;
    var feeBalanceType
    var especiallyFee

    function detailServiceReportFee() {
        var self = plus.webview.currentWebview();
        console.log(JSON.stringify(app.getUser()))
        ajaxGet(BASE_URL + 'cc/service/report/detailServiceReportFee', {
            serviceNo: h('#serviceNo').html(),
            helpId: h('#helpId').val(),
            engineerId: app.getUser().enginnerId,
            bu: h('#bu').val(),
            prov: h('#prov').val(),
            city: h('#city').val(),
            chargeHours: self.chargeHours
        }, function (data) {
            if (data.code === 200) {
                var minHourCC = 0;
                var hourFreeCC;
                var basicFeeCC;
                // if (data.data.checkState == 1){
                //0 旅途住宿/1基本费/2基本费+特殊费
                feeBalanceType = data.data.feeBalanceType || 0
                // feeBalanceType = 2
                especiallyFee = data.data.especiallyFee || 0
                h('#manHours').val(data.data.manHours) //数量/天/小时
                h('#manRate').val(data.data.manRate) //单价
                h('#totalPrice').val(data.data.totalPrice) //总计
                h('#travelStayPrice').val(data.data.travelStayPrice) //旅途和住宿费
                h('#basicFee').val(data.data.basicFee) //基本费
                h('#manTotal').val(data.data.manTotal) //旅途和人工费
                h('#especiallyFee').val(data.data.especiallyFee) //特殊费用
                h('#partsTotallFee').val(data.data.partsTotallFee) //备件总费用
                h('#totalBilled').val(data.data.totalBilled) //总费用

                if (feeBalanceType == '0') {
                    //0 旅途住宿
                    zhuangtai = true;
                    h('#divTravelStayPrice').show()
                    shifoujisuan = false;
                } else if (feeBalanceType == '1') {
                    //1基本费
                    zhuangtai = true;
                    // h('#basicFee').parent().show()
                    // h('#basicFee').parent().show()
                    h('#divBasicFee').show()
                    disableInput()
                    h('#btn_box').hide()
                } else if (feeBalanceType == '2') {
                    //2基本费+特殊费
                    zhuangtai = true;
                    h('#divBasicFee').show()
                    h('#divEspeciallyFee').show()
                    h('#especiallyFee').val(especiallyFee)
                    h('#btn_box').hide()
                    var basic = data.data && data.data.basicFee;
                    // var basic=0
                    if (Number(basic)) {
                        disableInput()
                    } else {
                        h('#divBasicFee').hide()
                        h('#divTravelStayPrice').show()
                        shifoujisuan = false;
                        h('#btn_box').show()
                        var manRateVal = h('#manRate').val()
                        if (manRateVal == '' || manRateVal == 0) {

                            h('#manRate').val('')

                        } else {
                            h('#manRate').val(manRateVal)
                        }
                        var manHoursVal = h('#manHours').val()
                        if (manHoursVal == '' || manHoursVal == 0) {
                            h('#manHours').val('')
                        } else {
                            h('#manHours').val(manHoursVal)
                        }
                        var travelStayPrice = h('#travelStayPrice').val()
                        if (travelStayPrice == '' || travelStayPrice == 0) {
                            h('#travelStayPrice').val('')
                        } else {
                            h('#travelStayPrice').val(travelStayPrice)
                        }
                    }
                    // h('#basicFee').parent().show()
                    // h('#basicFee').parent().show()
                    // h('#especiallyFee').parent().show()
                    // h('#especiallyFee').parent().show()

                }

                // }


                //"Travel and Hotel 旅途和住宿费"/(Basic Price 基本费)+"Labor 人工——Total 总计"

                // if (data.data.ccState == '1' && data.data.ccLength > 0) {
                //“basic_fee”:”1980”,” hour_fee”:”529” ,” min_hour”:”4”
                /*console.log(data.data)
                minHourCC = data.data.min_hour || 0
                hourFreeCC = data.data.hour_fee
                basicFeeCC = data.data.basic_fee

                var manHours = 0;
                var manHoursDto = data.data.manHours || 0;
                if (!shifoujisuan) {
                    if (manHoursDto > 0) {
                        h('#manHours').val(manHoursDto);
                        h('#manRate').val(data.data.manRate || 0);
                        hourFreeCC = data.data.manRate || 0;
                    } else {
                        h('#manHours').val('');
                        h('#manRate').val('');
                        hourFreeCC = 0;
                    }
                    h('#basicFee').val('0');
                    basicFeeCC = 0;
                } else {
                    manHours = h('#chargeHours').val() - minHourCC
                    if (manHours >= 0) {
                        h('#manHours').val(manHours);
                        h('#manHours').attr("readonly", "readonly");
                    } else {
                        h('#manHours').val('0');
                    }
                    h('#manRate').val(hourFreeCC);
                    h('#manRate').attr("readonly", "readonly");
                    h('#basicFee').val(basicFeeCC);
                    h('#basicFee').attr("readonly", "readonly");
                }
                manHours = h('#manHours').val();

                var totalPrice = Number(hourFreeCC) * Number(manHours);
                h('#totalPrice').val(totalPrice);//人工费

                if (!shifoujisuan) {
                    var travelStayPrice = data.data.travelStayPrice || 0
                    h('#travelStayPrice').val(data.data.travelStayPrice || '')
                    var manTotal = Number(totalPrice) + Number(travelStayPrice);
                    h('#manTotal').val(manTotal);//旅途和人工费
                } else {
                    // 显示字段5：Travel and Labor 旅途和人工费 = "Labor 人工——Total 总计"+"Basic Price 基本费" = 640+1280 = 1920
                    var manTotal = Number(totalPrice) + Number(basicFeeCC);
                    h('#manTotal').val(manTotal);//旅途和人工费
                }

                var partsTotallFee = data.data.partsTotallFee || 0;
                h('#partsTotallFee').val(partsTotallFee);

                var especiallyFee = data.data.especiallyFee;
                if (especiallyFee == -1) {
                    especiallyFee = h("#especiallyFee").val() || 0.0;
                }
                h("#especiallyFee").val(Number(especiallyFee))//特殊费用


                h('#totalBilled').val(Number(manTotal) + Number(partsTotallFee) + Number(especiallyFee));
*/


                /*else {
                    var manHoursDto = data.data.manHours || 0;
                    if (!shifoujisuan) {
                        if (manHoursDto > 0) {
                            h('#manHours').val(manHoursDto);
                        } else {
                            h('#manHours').val('0');
                        }
                    } else {
                        h('#manHours').val(h('#chargeHours').val());
                    }

                    var chargeHours = h('#manHours').val()
                    var manRate = data.data.manRate || 0;
                    h('#manRate').val(data.data.manRate || '');
                    var totalPrice = Number(chargeHours) * Number(manRate);
                    h('#totalPrice').val(totalPrice);
                    var travelStayPrice = data.data.travelStayPrice || 0;
                    h('#travelStayPrice').val(data.data.travelStayPrice || '')
                    var basicFee = data.data.basicFee || 0;
                    h('#basicFee').val(data.data.basicFee || '');
                    var partsTotallFee = data.data.partsTotallFee || 0;
                    h('#partsTotallFee').val(partsTotallFee);
                    if (!shifoujisuan) {
                        var manTotal = Number(totalPrice) + Number(travelStayPrice);
                        h('#manTotal').val(manTotal);
                    } else {
                        var manTotal = Number(totalPrice) + Number(basicFee);
                        h('#manTotal').val(manTotal);
                    }

                    var especiallyFee = data.data.especiallyFee;
                    if (especiallyFee == -1) {
                        especiallyFee = h("#especiallyFee").val() || 0.0;
                    }
                    h("#especiallyFee").val(especiallyFee)
                    h('#totalBilled').val(Number(manTotal) + Number(especiallyFee) + Number(partsTotallFee));
                }*/
                // }
                /*else {
                    if (!zhuangtai) {
                        h('#travelStayPrice').parent().show()
                        h('#travelStayPrice').parent().show()
                        shifoujisuan = false;
                    }

                    var manHoursDto = data.data.manHours || 0;
                    if (!shifoujisuan) {
                        if (manHoursDto > 0) {
                            h('#manHours').val(manHoursDto);
                        } else {
                            h('#manHours').val('0');
                        }
                    } else {
                        h('#manHours').val(manHoursDto);
                    }

                    var chargeHours = h('#manHours').val()
                    var manRate = data.data.manRate || 0;
                    h('#manRate').val(data.data.manRate || 0);
                    var totalPrice = Number(chargeHours) * Number(manRate);
                    h('#totalPrice').val(totalPrice); //人工总费用 Total总计

                    var basicFee = data.data.basicFee || 0;
                    h('#basicFee').val(data.data.basicFee || 0);



                    var travelStayPrice = data.data.travelStayPrice || 0;
                    h('#travelStayPrice').val(travelStayPrice) //Travel and Hotel 旅途和住宿费


                    var partsTotallFee = data.data.partsTotallFee || 0;
                    h('#partsTotallFee').val(partsTotallFee);

                    if (!shifoujisuan) {
                        var manTotal = Number(totalPrice) + Number(travelStayPrice);
                        h('#manTotal').val(manTotal);
                    } else {
                        var manTotal = Number(totalPrice) + Number(basicFee);
                        h('#manTotal').val(manTotal);
                    }

                    var especiallyFee = data.data.especiallyFee?data.data.especiallyFee:0;
                    if (especiallyFee == -1) {
                        especiallyFee = h("#especiallyFee").val() || 0.0;
                    }
                    h("#especiallyFee").val(Number(especiallyFee))//特殊费用


                    //报告总费用 Total to billed总费用
                    h('#totalBilled').val(Number(manTotal) + Number(especiallyFee) + Number(partsTotallFee));
                }*/

                if (submitStatus == 0) {

                    if (feeBalanceType == "" || feeBalanceType == 0) {
                        var manRateVal = h('#manRate').val()
                        if (manRateVal == '' || manRateVal == 0) {

                            h('#manRate').val('')

                        } else {
                            h('#manRate').val(manRateVal)
                        }
                        var manHoursVal = h('#manHours').val()
                        if (manHoursVal == '' || manHoursVal == 0) {
                            h('#manHours').val('')
                        } else {
                            h('#manHours').val(manHoursVal)
                        }
                        var travelStayPrice = h('#travelStayPrice').val()
                        if (travelStayPrice == '' || travelStayPrice == 0) {
                            h('#travelStayPrice').val('')
                        } else {
                            h('#travelStayPrice').val(travelStayPrice)
                        }
                    }
                }

                h('#mui-content').show()

            } else {
                if (data.msg) {
                    app.toast(data.msg)
                } else {
                    app.toast()
                }
            }
            if (h('#chargeHours').val() == '0' && Number(data.data.basicFee)) {
                h('#basicFee').val(0)
                calculateCost()
                disableInput()
            }
            calculateCost()
        }, function (errorMsg) {
            app.toast(errorMsg)
        });
    }

    function calculateCost() {
        var manHoursTmp = h('#manHours').val() || 0.0;
        var manRateTmp = h('#manRate').val() || 0.0;
        var basicFeeTmp = h('#basicFee').val() || 0.0;
        var partsTotallFeeTmp = h('#partsTotallFee').val() || 0.0;
        var especiallyFeeTmp = h("#especiallyFee").val() || 0.0;
        var travelStayPriceTmp = h("#travelStayPrice").val() || 0.0;
        var checkInt = /^\d+(?=\.{0,1}\d+$|$)/;
        if (!checkInt.test(manHoursTmp) || !checkInt.test(manRateTmp) || !checkInt.test(basicFeeTmp) || !checkInt.test(partsTotallFeeTmp) || !checkInt.test(especiallyFeeTmp)) {
            app.toast('不可以输入负数')
            return;
        }

        var totalPriceTmp = Number(manHoursTmp) * Number(manRateTmp);
        h('#totalPrice').val(totalPriceTmp);

        if (!shifoujisuan) {
            var manTotalTmp = Number(totalPriceTmp) + Number(travelStayPriceTmp);
            h('#manTotal').val(manTotalTmp);
        } else {
            var manTotalTmp = Number(totalPriceTmp) + Number(basicFeeTmp);
            h('#manTotal').val(manTotalTmp);
        }

        var totalBilledTmp = Number(manTotalTmp) + Number(especiallyFeeTmp) + Number(partsTotallFeeTmp);
        h('#totalBilled').val(totalBilledTmp);
    }


    // 提交状态 0未提交 1已提交
    var submitStatus = 0;

    /*
    * 禁止交互事件
    */
    function noInteraction() {
        h('#especiallyFee').attr('readonly', 'readonly')

        if (submitStatus && submitStatus == '1') {
            disableInput()

            h('#saveBtn').hide()
            h('#cancale').hide()
        }
    }

    function disableInput() {
        h('#manHours').attr('readonly', 'readonly')
        h('#manRate').attr('readonly', 'readonly')
        h('#totalPrice').attr('readonly', 'readonly')
        h('#travelStayPrice').attr('readonly', 'readonly')
        h('#manTotal').attr('readonly', 'readonly')
        h('#basicFee').attr('readonly', 'readonly')
        h('#partsTotallFee').attr('readonly', 'readonly')
        h('#totalBilled').attr('readonly', 'readonly')
        h('#especiallyFee').attr('readonly', 'readonly')
    }

    (function ($, doc) {
        $.init();

        $.plusReady(function () {
            var self = plus.webview.currentWebview();
            var wo = self.opener();
            submitStatus = self.submitStatus
            noInteraction()
            h('#serviceNo').html(self.serviceNo);
            h('#id').val(self.serviceReportId);
            h('#enginnerId').val(self.enginnerId);
            h('#helpId').val(self.helpId);
            h('#bu').val(self.bu);
            h('#city').val(self.city);
            h('#prov').val(self.prov);
            chargeHours = self.chargeHours;
            h('#chargeHours').val(chargeHours);
            var noticeId = self.noticeId;
            h("#noticeId").val(noticeId);

            getDetails()


            h('#saveBtn').tap(function () {
                if (!app.getNetworkState()) {
                    app.toast("无网络")
                    return
                }
                var id = h('#id').val();
                var serviceNo = h('#serviceNo').html();
                var helpId = h('#helpId').val();
                var basicFee = h('#basicFee').val();
                var manRate = h('#manRate').val();
                var manHours = h('#manHours').val();
                //var manTotal = h('#manTotal').val() ? h('#manTotal').val() : h('#travelStayPrice').val();
                var totalPrice = h('#totalPrice').val(); //人工总费用 Total总计

                var manTotal = h('#manTotal').val();//人工总费用 Total总计


                var especiallyFee = h('#especiallyFee').val();//特殊费用
                var travelStayPrice = h('#travelStayPrice').val();
                var partsTotallFee = h('#partsTotallFee').val();//备件总费用
                var totalBilled = h('#totalBilled').val(); //报告总费用 Total to billed总费用
                var chargeHours = self.chargeHours

                var enginnerId = app.getUser().enginnerId
                var fseName = app.getUser().name;
                if (!totalPrice) {
                    app.toast('总计不能为空')
                    return
                }
                if (!manTotal) {
                    app.toast('旅途和人工费不能为空')
                    return
                }
                if (shifoujisuan && !basicFee) {
                    app.toast('基本费不能为空')
                    return
                }
                if (!partsTotallFee) {
                    app.toast('PartsTotallFee不能为空')
                    return
                }
                if (!totalBilled) {
                    app.toast('总费用不能为空')
                    return
                }
                var params = {
                    id,
                    serviceNo,
                    helpId,
                    basicFee,
                    manRate,
                    manHours,
                    manTotal,
                    partsTotallFee,
                    totalBilled,
                    totalPrice,
                    travelStayPrice,
                    enginnerId,
                    fseName,
                    especiallyFee,
                    feeBalanceType,
                    chargeHours
                }
                console.log(params)
                app.showWaiting('正在保存...')
                ajaxPost(BASE_URL + 'cc/service/report/editServiceFee', params, function (data) {
                    app.closeWaiting()
                    console.log(JSON.stringify(data))
                    if (data.code === 200) {
                        h('#id').val(data.data.id)
                        app.toast("保存成功")
                        wo.evalJS('updataFee("' + data.data.id + '")')
                        mui.back()
                    } else {
                        app.toast()
                    }
                }, function (errorMsg) {
                    app.closeWaiting()
                    app.toast(errorMsg)
                });

            });

            h('#cancale').tap(function () {
                $.back()
            });

        });
    }(mui, document));


    function getDetails() {
        ajaxGet(BASE_URL + "cc/notice/getDaiZhiPaiNoticeDetail", {
            "ServiceNoticeId": h("#noticeId").val()
        }, function (data) {
            // console.log(JSON.stringify(data))
            if (data.code == 200) {
                if (data.data != "") {

                    if (data.data.serviceType == undefined || data.data.serviceType == "") {
                        serviceType = "";
                    } else {
                        serviceType = data.data.serviceType
                    }

                    if (data.data.subServiceType == undefined || data.data.subServiceType == "") {
                        subServiceType = "";
                    } else {
                        subServiceType = data.data.subServiceType
                    }

                    if (data.data.isWarranty == undefined || data.data.isWarranty == "") {
                        isWarranty = "";
                    } else {
                        isWarranty = data.data.isWarranty
                    }
                    // console.log("serviceType===:"+serviceType+"  subServiceType===:"+subServiceType+"  isWarranty===:"+isWarranty)
                    detailServiceReportFee()
                }

            }
        }, function (errorMsg) {
            console.log(JSON.stringify(errorMsg))
        });
    }

</script>
<!-- js代码 end -->

</body>

</html>
